#pragma once
#include "Manager.h"
#include <iostream>
#include "msclr\marshal_cppstd.h"


namespace CS2103v01 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for EditCategoryName
	/// </summary>
	public ref class EditCategoryName : public System::Windows::Forms::Form
	{
	public:
		EditCategoryName(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			TM = Manager::get();
			_cat="";
			label3->Text="";
			setUpComboBox("cat", comboBox1);
			//
		}		
		EditCategoryName(String^ cat)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			TM = Manager::get();
			_cat = cat;
			label3->Text = "";
			setUpComboBox("cat", comboBox1);
			if(_cat!="")
			{
				int i = comboBox1->FindString(_cat);
				comboBox1->SelectedIndex = i;
			}
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~EditCategoryName()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::ComboBox^  comboBox1;

	private:
		/// <summary>
		/// Required designer variable.
		Manager *TM;
		String^ _cat;
	private: System::Windows::Forms::Label^  label3;
			 /// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->button1->Location = System::Drawing::Point(267, 151);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"OK";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &EditCategoryName::button1_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label1->Location = System::Drawing::Point(60, 53);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(61, 17);
			this->label1->TabIndex = 1;
			this->label1->Text = L"Category";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label2->Location = System::Drawing::Point(61, 100);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(130, 17);
			this->label2->TabIndex = 2;
			this->label2->Text = L"New Category Name";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(197, 98);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 21);
			this->textBox1->TabIndex = 3;
			// 
			// comboBox1
			// 
			this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Location = System::Drawing::Point(197, 51);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(121, 20);
			this->comboBox1->TabIndex = 4;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label3->ForeColor = System::Drawing::Color::Red;
			this->label3->Location = System::Drawing::Point(197, 119);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(39, 16);
			this->label3->TabIndex = 5;
			this->label3->Text = L"label3";
			// 
			// EditCategoryName
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(378, 207);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button1);
			this->Name = L"EditCategoryName";
			this->Text = L"Edit Category Name";
			this->Activated += gcnew System::EventHandler(this, &EditCategoryName::EditCategoryName_Activated);
			this->Load += gcnew System::EventHandler(this, &EditCategoryName::EditCategoryName_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		//@author A0086933U
	private: System::Void EditCategoryName_Load(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void EditCategoryName_Activated(System::Object^  sender, System::EventArgs^  e) {
				 if(TM->findCategory(msclr::interop::marshal_as<std::string>(System::Convert::ToString(_cat))) == TM->getCatAddr()->end())
				 {
					 _cat="";
					 label3->Text="";
					 setUpComboBox("cat", comboBox1);
				 }
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 if(!checkField(textBox1, label3))
					 return;

				 string NEW="", OLD="";
				 NEW = msclr::interop::marshal_as<std::string>(textBox1->Text); 
				 OLD = msclr::interop::marshal_as<std::string>(comboBox1->Text);

				 if(TM->editCategory(OLD, NEW))
				 {
					 TM->clearActionStack();
					 this->Close();
				 }
				 else
					 label3->Text="Existed";
			 }

	private: void setUpComboBox(string type, ComboBox^CB)
			 {
				 if(type=="acc")
				 {
					 CB->Items->Clear();
					 if(!((TM->getAccAddr())->empty()))
					 {
						 list<string>::iterator iterA;
						 String ^ tempAcc;
						 for(iterA = (TM->getAccAddr())->begin(); iterA != (TM->getAccAddr())->end(); iterA++)
						 {
							 if(*iterA!="delete")
							 {
								 tempAcc =  msclr::interop::marshal_as<System::String^>(*iterA);
								 CB->Items->Add(tempAcc);
							 }
						 }
						 CB->SelectedIndex = 0;
					 }
				 }
				 else if(type=="cat")
				 {
					 CB->Items->Clear();

					 if(!((TM->getCatAddr())->empty()))
					 {
						 list<string>::iterator iterC = TM->getCatAddr()->begin();
						 String ^ tempCat;
						 for(; iterC != (TM->getCatAddr())->end(); iterC++)
						 {
							 if(*iterC!="Income" && *iterC!="Transfer" && *iterC!="Others")
							 {
								 tempCat =  msclr::interop::marshal_as<System::String^>(*iterC);
								 CB->Items->Add(tempCat);
							 }
						 }
						 CB->SelectedIndex = 0;
					 }
				 }
				 else if(type=="hour")
				 {
					 struct tm *sysTime;
					 time_t *rawSysTime;
					 rawSysTime = new time_t;
					 time(rawSysTime);
					 sysTime = localtime(rawSysTime);
					 CB->SelectedIndex = sysTime->tm_hour;
					 delete rawSysTime;
				 }
				 else if(type=="min")
				 {
					 struct tm *sysTime;
					 time_t *rawSysTime;
	 				 rawSysTime = new time_t;
					 time(rawSysTime);
					 sysTime = localtime(rawSysTime);
					 CB->SelectedIndex = sysTime->tm_min;
					 delete rawSysTime;
				 }

				 else
					 return;
			 }
	private: bool checkField(TextBox^ cat, Label^ catL)
			 {
				 catL->Text="";
				 bool miss=false;
				 bool format=true;

				 //check empty field
				 if(cat->Text=="")
				 {
					 catL->Text="missing";
					 miss=true;
				 }
				 if(miss)
					 return false;
				 
				 //check format
				 return format;
			 }
};
}
